package cn.wolfcode.web.controller;

import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.ISeckillProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/seckillProduct")
@Slf4j
public class SeckillProductController {
    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/selectByTime")
    public Result<List<SeckillProductVo>> seelectByTime(@RequestParam("time") int time) {
        return Result.success(seckillProductService.selectByTime(time));
    }

    /**
     * 根据前台传入的场次从redis中查询对应的秒杀商品的数据
     * @param time   场次
     * @return
     */
    @RequestMapping("/queryByTime")
    public Result<List<SeckillProductVo>> queryByTime(@RequestParam("time") int time) {
        return Result.success(seckillProductService.queryByTime(time));
    }

    /**
     * 根据传入的场次和秒杀商品的Id从Redis中查询对应的数据
     *
     * @param seckillId 场次
     * @param time      秒杀商品Id
     * @return
     */
    @RequestMapping("/find")
    public Result<SeckillProductVo> find(Long seckillId, int time) {
        SeckillProductVo vo = seckillProductService.find(seckillId, time);
        /*String objStr = (String) redisTemplate.opsForHash().get(SeckillRedisKey.SECKILL_REAL_COUNT_HASH.getRealKey(String.valueOf(time)), String.valueOf(seckillId));
        int currentCount = Integer.parseInt(objStr);
        vo.setCurrentCount(currentCount>=0?currentCount:0);*/
        return Result.success(vo);
    }
}
